package com.tianpengtech.front.web.fair;

import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.common.interceptor.PassportInterceptor;
import com.tianpengtech.front.web.task.TaskService;

import java.util.HashMap;
import java.util.Map;

/**
 * Controller for fair management.
 * @author Delicate
 */
@Clear(PassportInterceptor.class)
public class FairWebController extends BaseController {
    private FairService fairService = new FairService();
    private TaskService taskService = new TaskService();
    private DictionaryDao dictionaryDao = new DictionaryDao();
    public void index(){
        setAttr("region", Db.findByCache(Fair.FAIR_CACHE_NAME,Fair.FAIR_CACHE_REGION,"select REGION_ID,REGION_NAME from region where PARENT_ID=1"));
        setAttr("platform",dictionaryDao.readParents(10055));
        setAttr("channel",dictionaryDao.readParents(10056));
        render("fair_list.html");
    }


    /**
     * 选择省之后查询对应的市
     */
    public void region(){
        String id=getPara("id");
        if (StrKit.isBlank(id)){
            renderJson(Ret.fail("msg","id不能为空"));
            return;
        }
        renderJson(Db.findByCache(Fair.FAIR_CACHE_NAME,Fair.FAIR_CACHE_REGION+id,"select REGION_ID,REGION_NAME from region where PARENT_ID=?",id));
    }




    /**
     * 网红选择页面
     */
    public void list(){
        int pageNum = getParaToInt(GlobalConfigConstant.PARAM_NAME_PAGENUM,
                GlobalConfigConstant.DEFAULT_VALUE_PAGENUM);
        int pageSize = getParaToInt(GlobalConfigConstant.PARAM_NAME_PAGESIZE,
                GlobalConfigConstant.DEFAULT_VALUE_PAGENUM);
        Map<String, Object> params = new HashMap<>();
        params.put(FairService.PARAM_NICKNAME,getPara(FairService.PARAM_NICKNAME));
        params.put(FairService.PARAM_PROVONCE,getPara(FairService.PARAM_PROVONCE));
        params.put(FairService.PARAM_CITY,getPara(FairService.PARAM_CITY));
        params.put(FairService.PARAM_PLATFORM,getPara(FairService.PARAM_PLATFORM));
        params.put(FairService.PARAM_CHANNEL,getPara(FairService.PARAM_CHANNEL));
        Page<Fair> results =fairService.fair(pageNum, pageSize, params);
        renderJson(results);
    }

    /**
     * 查看
     */
    public void detail(){
        String id=getPara("id");
        Fair fair=null;
        if (StrKit.notBlank(id)){
            fair=fairService.findById(Integer.parseInt(id));
            if (fair==null){
                renderJson(Ret.fail("msg","网红不存在"));
                return;
            }
        }else{
            renderJson(Ret.fail("msg","网红不存在"));
            return;
        }
        setAttr("fair",fair);
        renderTemplate("fair_detail.html");
    }

    /**
     * 网红详情分页获取网红任务列表
     */
    public void detail_task(){
        String id=getPara("id");
        int start = getParaToInt(GlobalConfigConstant.PARAM_NAME_START,
                5);
        int length = getParaToInt(GlobalConfigConstant.PARAM_NAME_LENGTH,
                GlobalConfigConstant.DEFAULT_VALUE_PAGESIZE);
        int pageNum = XxxUtil.calculatePageNumForDataTable(start, length);
        if (StrKit.isBlank(id)) {
            renderJson(Ret.fail("msg", "网红不存在"));
            return;
        }
        setAttr("taskPage",taskService.findTaskByFair(pageNum,length,Integer.parseInt(id)));
        renderJson();
    }
}
